package com.frejoys.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.admin.*;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.enums.EStoreType;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.admin.*;
import com.frejoys.dao.entity.Store;
import com.frejoys.dao.entity.StoreStatisticsYear;
import com.frejoys.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Tag(name = "店铺管理", description = "店铺管理")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixAdmin)
@Authorization(Authority.Private)
public class AdminStoreController {

    // 店家service
    private final StoreService storeService;

    private final CategoryService categoryService;

    private final RedissonClient redissonClient;

    private final StoreSaleService storeSaleService;

    private final StoreStatisticsDayService storeStatisticsDayService;

    private final StoreStatisticsMonthService storeStatisticsMonthService;

    private final StoreStatisticsYearService storeStatisticsYearService;

    @Operation(summary = "店铺列表")
    @PostMapping("/store/list")
    public Result<IPage<StoreVo>> storeList(@RequestBody @Validated StoreListDto dto) {
        return Result.success(storeService.listInfo(dto));
    }

    @Operation(summary = "店铺列表加线下码信息")
    @PostMapping("/store/storeListInfo")
    public Result<IPage<StoreAndCodeVo>> storeListInfo(@RequestBody @Validated StoreListDto dto) {
        return Result.success(storeService.storeListInfo(dto));
    }

    @Operation(summary = "店铺详情")
    @PostMapping("/store/detail")
    public Result<StoreVo> detail(@RequestBody @Validated IdDto dto) {
        Store store = storeService.getById(dto.getId());
        StoreVo storeVo = BeanUtil.copyProperties(store, StoreVo.class);
        if (storeVo != null) {
            Map<Integer, String> categoryIdNameMap = categoryService.getIdNameMap(Collections.singletonList(storeVo.getCategoryId()));
            storeVo.setCategoryName(categoryIdNameMap.get(storeVo.getCategoryId()));
        }
        return Result.success(storeVo);
    }

    @Operation(summary = "添加店铺")
    @PostMapping("/store/addInfo")
    public Result addInfo(@RequestBody @Validated StoreAddDto dto) {
        storeService.addInfo(dto, RequestUtil.userId());
        return Result.success();
    }

    @Operation(summary = "编辑店铺")
    @PostMapping("/store/upInfo")
    public Result upInfo(@RequestBody @Validated StoreUpDto dto) {
        storeService.upInfo(dto);
        return Result.success();
    }

    @Operation(summary = "获取店铺类型")
    @PostMapping("/store/storeTypeList")
    public Result<List<StoreTypeSelectVo>> storeTypeList() {
        EStoreType[] values = EStoreType.values();
        List<StoreTypeSelectVo> voList = Arrays.stream(values).map(e -> {
            StoreTypeSelectVo vo = new StoreTypeSelectVo();
            vo.setText(e.getName());
            vo.setValue(e.getValue());
            return vo;
        }).toList();
        return Result.success(voList);
    }



    @Operation(summary = "删除店铺")
    @PostMapping("/store/delInfo")
    public Result delInfo(@RequestBody @Validated IdDto dto) {
        storeService.delInfo(dto);
        return Result.success();
    }

    @Operation(summary = "修改店铺上下架状态")
    @PostMapping("/store/upStoreShelves")
    public Result upStoreShelves(@RequestBody @Validated UpShelvesDto dto) {

        RLock lock = redissonClient.getLock(CacheKeyUtil.storeEditLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {

                storeService.upStoreShelves(dto);

            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }


    @Operation(summary = "店铺销售列表")
    @PostMapping("/store/storeSaleList")
    public Result<IPage<StoreSaleListVo>> storeSaleList(@RequestBody @Validated StoreSaleListAndPageDto dto) {
        return Result.success(storeSaleService.storeSaleList(dto));
    }


    @Operation(summary = "店铺销售日统计列表")
    @PostMapping("/store/storeStatisticsDayList")
    public Result<IPage<StoreStatisticsListVo>> storeStatisticsDayList(@RequestBody @Validated StoreSaleListAndPageDto dto) {
        return Result.success(storeStatisticsDayService.storeStatisticsDayList(dto));
    }

    @Operation(summary = "店铺销售月统计列表")
    @PostMapping("/store/storeStatisticsMonthList")
    public Result<IPage<StoreStatisticsListVo>> storeStatisticsMonthList(@RequestBody @Validated StoreSaleListAndPageDto dto) {
        return Result.success(storeStatisticsMonthService.storeStatisticsMonthList(dto));
    }

    @Operation(summary = "店铺销售年统计列表")
    @PostMapping("/store/storeStatisticsYearList")
    public Result<IPage<StoreStatisticsListVo>> storeStatisticsYearList(@RequestBody @Validated StoreSaleListAndPageDto dto) {
        return Result.success(storeStatisticsYearService.storeStatisticsYearList(dto));
    }

    @PostMapping("/store/storeNameList")
    @Operation(summary = "获取店铺名字和logo列表")
    public Result<IPage<StoreIdAndNameListVo>> storeNameList(@RequestBody @Validated StoreIdAndNameDto dto){
        return Result.success(storeService.storeNameList(dto));
    }
}
